package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.LineTechBigType;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParam;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceParamType;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbaDeviceParamDao {

    /**
     * 查询所有
     *
     * @return
     */
    @Results(id = "pcbaDeviceParamResultMap", value = {
            @Result(column = "pcba_device_param_id", property = "pcbaDeviceParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_name_cn", property = "pcbaDeviceParamNameCn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_param_name_en", property = "pcbaDeviceParamNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_param_default", property = "pcbaDeviceParamDefault", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_key", property = "pcbaDeviceParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_param_fill_type", property = "pcbaDeviceParamFillType", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_options", property = "pcbaDeviceParamOptions", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_required", property = "isRequired", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_type_id", property = "pcbaDeviceParamTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_param_placeholder", property = "pcbaDeviceParamPlaceholder", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_device_param_picture", property = "pcbaDeviceParamPicture", jdbcType = JdbcType.VARCHAR),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_related_pcba", property = "isRelatedPcba", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where 1 = 1 " +
            "<if test='searchPcbaDeviceParamName != null and searchPcbaDeviceParamName != \"\" '>" +
            " and (pcba_device_param_name_cn like CONCAT('%',#{searchPcbaDeviceParamName},'%') or pcba_device_param_name_en like CONCAT('%',#{searchPcbaDeviceParamName},'%'))" +
            "</if>" +
            "<if test='pcbaDeviceTypeId != null and pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id = #{pcbaDeviceTypeId}" +
            "</if>" +
            "<if test='pcbaDeviceParamTypeId != null and pcbaDeviceParamTypeId != \"\" '>" +
            " and pcba_device_param_type_id = #{pcbaDeviceParamTypeId}" +
            "</if>" +
            " order by pcba_device_param_type_id,sort" +
            "</script>")
    List<PcbaDeviceParam> getAllPcbaDeviceParam(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId, @Param("searchPcbaDeviceParamName") String searchPcbaDeviceParamName);

    /**
     * 新增
     *
     * @param pcbaDeviceParam
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_DEVICE_PARAM + "(pcba_device_param_name_cn, pcba_device_param_name_en,pcba_device_param_default,create_time,sort,pcba_device_type_id,status,pcba_device_param_key,is_required,pcba_device_param_fill_type,pcba_device_param_options,pcba_device_param_type_id,pcba_device_param_placeholder,pcba_device_param_picture,notes,is_related_pcba) values " +
            "(#{pcbaDeviceParam.pcbaDeviceParamNameCn}, #{pcbaDeviceParam.pcbaDeviceParamNameEn},#{pcbaDeviceParam.pcbaDeviceParamDefault},#{pcbaDeviceParam.createTime},#{pcbaDeviceParam.sort},#{pcbaDeviceParam.pcbaDeviceTypeId},#{pcbaDeviceParam.status},#{pcbaDeviceParam.pcbaDeviceParamKey},#{pcbaDeviceParam.isRequired},#{pcbaDeviceParam.pcbaDeviceParamFillType},#{pcbaDeviceParam.pcbaDeviceParamOptions},#{pcbaDeviceParam.pcbaDeviceParamTypeId},#{pcbaDeviceParam.pcbaDeviceParamPlaceholder},#{pcbaDeviceParam.pcbaDeviceParamPicture},#{pcbaDeviceParam.notes},#{pcbaDeviceParam.isRelatedPcba})")
    Integer addPcbaDeviceParam(@Param("pcbaDeviceParam") PcbaDeviceParam pcbaDeviceParam);

    /**
     * 更新
     *
     * @param pcbaDeviceParam
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_DEVICE_PARAM +
            "<set>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamNameCn != null and pcbaDeviceParam.pcbaDeviceParamNameCn != \"\" '>" +
            " pcba_device_param_name_cn = #{pcbaDeviceParam.pcbaDeviceParamNameCn}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamNameEn != null and pcbaDeviceParam.pcbaDeviceParamNameEn != \"\" '>" +
            " pcba_device_param_name_en = #{pcbaDeviceParam.pcbaDeviceParamNameEn}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamDefault != null and pcbaDeviceParam.pcbaDeviceParamDefault != \"\" '>" +
            " pcba_device_param_default = #{pcbaDeviceParam.pcbaDeviceParamDefault}," +
            "</if>" +
            "<if test='pcbaDeviceParam.updateTime != null '>" +
            " update_time = #{pcbaDeviceParam.updateTime}," +
            "</if>" +
            "<if test='pcbaDeviceParam.sort != null and pcbaDeviceParam.sort != \"\" '>" +
            " sort = #{pcbaDeviceParam.sort}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceTypeId != null and pcbaDeviceParam.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaDeviceParam.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaDeviceParam.status != null and pcbaDeviceParam.status != \"\" '>" +
            " status = #{pcbaDeviceParam.status}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamKey != null and pcbaDeviceParam.pcbaDeviceParamKey != \"\" '>" +
            " pcba_device_param_key = #{pcbaDeviceParam.pcbaDeviceParamKey}," +
            "</if>" +
            "<if test='pcbaDeviceParam.isRequired != null and pcbaDeviceParam.isRequired != \"\" '>" +
            " is_required = #{pcbaDeviceParam.isRequired}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamFillType != null and pcbaDeviceParam.pcbaDeviceParamFillType != \"\" '>" +
            " pcba_device_param_fill_type = #{pcbaDeviceParam.pcbaDeviceParamFillType}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamOptions != null and pcbaDeviceParam.pcbaDeviceParamOptions != \"\" '>" +
            " pcba_device_param_options = #{pcbaDeviceParam.pcbaDeviceParamOptions}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamTypeId != null and pcbaDeviceParam.pcbaDeviceParamTypeId != \"\" '>" +
            " pcba_device_param_type_id = #{pcbaDeviceParam.pcbaDeviceParamTypeId}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamPlaceholder != null '>" +
            " pcba_device_param_placeholder = #{pcbaDeviceParam.pcbaDeviceParamPlaceholder}," +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamPicture != null '>" +
            " pcba_device_param_picture = #{pcbaDeviceParam.pcbaDeviceParamPicture}," +
            "</if>" +
            "<if test='pcbaDeviceParam.notes != null '>" +
            " notes = #{pcbaDeviceParam.notes}," +
            "</if>" +
            "</set>" +
            " where pcba_device_param_id = #{pcbaDeviceParam.pcbaDeviceParamId}" +
            "</script>")
    Integer updatePcbaDeviceParam(@Param("pcbaDeviceParam") PcbaDeviceParam pcbaDeviceParam);

    /**
     * 根据条件查询
     *
     * @param pcbaDeviceParam
     * @return
     */
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where 1 = 1" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamNameCn != null and pcbaDeviceParam.pcbaDeviceParamNameCn != \"\" '>" +
            " and pcba_device_param_name_cn = #{pcbaDeviceParam.pcbaDeviceParamNameCn}" +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamNameEn != null and pcbaDeviceParam.pcbaDeviceParamNameEn != \"\" '>" +
            " and pcba_device_param_name_En = #{pcbaDeviceParam.pcbaDeviceParamNameEn}" +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceTypeId != null and pcbaDeviceParam.pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id = #{pcbaDeviceParam.pcbaDeviceTypeId}" +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamDefault != null and pcbaDeviceParam.pcbaDeviceParamDefault != \"\" '>" +
            " and pcba_device_param_default = #{pcbaDeviceParam.pcbaDeviceParamDefault}" +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamKey != null and pcbaDeviceParam.pcbaDeviceParamKey != \"\" '>" +
            " and pcba_device_param_key = #{pcbaDeviceParam.pcbaDeviceParamKey}" +
            "</if>" +
            "<if test='pcbaDeviceParam.pcbaDeviceParamId != null and pcbaDeviceParam.pcbaDeviceParamId != \"\" '>" +
            " and pcba_device_param_id = #{pcbaDeviceParam.pcbaDeviceParamId}" +
            "</if>" +
            "</script>")
    @ResultMap(value = "pcbaDeviceParamResultMap")
    PcbaDeviceParam getByPcbaDeviceParam(@Param("pcbaDeviceParam") PcbaDeviceParam pcbaDeviceParam);

    /**
     * 删除
     *
     * @param pcbaDeviceParamId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_DEVICE_PARAM + " where pcba_device_param_id = #{pcbaDeviceParamId}")
    Integer deleteById(@Param("pcbaDeviceParamId") Integer pcbaDeviceParamId);

    /**
     * 根据 pcbaDeviceParamTypeId 查询
     *
     * @param pcbaDeviceParamTypeId
     * @return
     */
    @ResultMap(value = "pcbaDeviceParamResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where pcba_device_param_type_id = #{pcbaDeviceParamTypeId} order by sort")
    List<PcbaDeviceParam> getByPcbaDeviceParamTypeId(@Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId);

    @ResultMap(value = "pcbaDeviceParamResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where pcba_device_param_key = #{pcbaDeviceParamKey}")
    PcbaDeviceParam getPcbaDeviceParamByKey(@Param("pcbaDeviceParamKey") String pcbaDeviceParamKey);

    /**
     * 根据id查询
     *
     * @param pcbaDeviceParamId
     * @return
     */
    @ResultMap(value = "pcbaDeviceParamResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where pcba_device_param_id = #{pcbaDeviceParamId}")
    PcbaDeviceParam getPcbaDeviceParamById(@Param("pcbaDeviceParamId") Integer pcbaDeviceParamId);

    @ResultMap(value = "pcbaDeviceParamResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where pcba_device_param_type_id = #{pcbaDeviceParamTypeId} and pcba_device_param_name_cn = #{pcbaDeviceParamNameCn}")
    PcbaDeviceParam getByNameAndPcbaDeviceParamTypeId(@Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId, @Param("pcbaDeviceParamNameCn") String pcbaDeviceParamNameCn);

    /**
     * 更新是否关联pcba能力判断
     *
     * @return
     */
    @Update("update " + DB1TableName.PCBA_DEVICE_PARAM + " set is_related_pcba = #{isRelatedPcba} where pcba_device_param_id = #{pcbaDeviceParamId}")
    Integer updateIsRelatedPcba(@Param("isRelatedPcba") Integer isRelatedPcba, @Param("pcbaDeviceParamId") Integer pcbaDeviceParamId);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "pcbaDeviceParamResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where pcba_device_param_name_cn = #{pcbaDeviceParamNameCn} and pcba_device_type_id = #{pcbaDeviceTypeId} and pcba_device_param_type_id = #{pcbaDeviceParamTypeId} " +
            "<if test='pcbaDeviceParamId != null and pcbaDeviceParamId != \"\" '>" +
            " and pcba_device_param_id != #{pcbaDeviceParamId}" +
            "</if>" +
            "</script>")
    List<PcbaDeviceParam> judgeUniquePcbaDeviceParam(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId, @Param("pcbaDeviceParamId") Integer pcbaDeviceParamId, @Param("pcbaDeviceParamNameCn") String pcbaDeviceParamNameCn);

    @ResultMap(value = "pcbaDeviceParamResultMap")
    @Select("select * from " + DB1TableName.PCBA_DEVICE_PARAM + " where pcba_device_type_id = #{pcbaDeviceTypeId} and pcba_device_param_type_id = #{pcbaDeviceParamTypeId} and pcba_device_param_name_cn = #{pcbaDeviceParamNameCn}")
    PcbaDeviceParam getPcbaDeviceParamByName(@Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("pcbaDeviceParamTypeId") Integer pcbaDeviceParamTypeId, @Param("pcbaDeviceParamNameCn") String pcbaDeviceParamNameCn);
}
